//登录 
const _login = async (that, name, password) => {
	const data = await that.$u.http.post('identityserver/Token/GetTokenByPwd?userName=' + name +
		'&pwd=' +
		password)
	const {
		Token: token,
		SystemId: systemId
	} = data
	if (token) {
		that.$u.vuex('vuex_token', token)
		//that.$u.vuex('vuex_key', key)
		that.$u.vuex('vuex_systemId', data.SystemId)
		return true
	} else {
		that.$refs.uToast.show({
			title: '账号或密码错误',
			type: 'warning',
		})
		return false
	}

}


//通过code 获取openId
const _getOpenId = (that) => {
	return new Promise((resolve, reject) => {
		wx.login({
			success(res) {
				if (res.code) {
					//发起网络请求	
					that.$u.http.post('Personnel/User/GetOpenID?code=' + res.code).then(res => {
						const {
							code,
							data: {
								systemId,
								openId,
								phoneNumber: phone
							}
						} = res
						console.log("获取用户信息1111",res)
						if (code == 0) {
							that.$u.vuex('vuex_openId', openId)
							//that.$u.vuex('vuex_key', key)
							that.$u.vuex('vuex_systemId', systemId)
							that.$u.vuex('vuex_phone', phone)
							_getToken(that)
						} else if (code == -1) {
							that.$u.vuex('vuex_openId', openId)
							// that.$refs.uToast.show({
							// 	title: res.msg,
							// 	type: 'warning',
							// })
							resolve(code)
						}
					})
				}
			}
		})
	})

}

//通过openId 获取 用户信息

const _getUserInfo = async (that, userName, password) => {
	
	const data = await that.$u.http.post('Personnel/User/LoginWX',{
			WXOpenId:that.vuex_openId,
			Account:userName,
			Password:password
		})
		console.log("登录之后返回的数据",data,that)
	if (data.code == 1) {
		that.$refs.uToast.show({
			title: data.msg,
			type: 'warning',
		})
	}
	const {
		code,
		data: {
			roleId,
			account,
			id,
			name,
			isAllowAttract,
			isAllowWX,
			phone
		}
	} = data
	uni.setStorageSync('roleId', roleId)
	that.$u.vuex('vuex_account', account)
	that.$u.vuex('vuex_password', password)
	that.$u.vuex('vuex_Id', id)
	that.$u.vuex('vuex_Name', name)
	that.$u.vuex('vuex_isAllowAttract', isAllowAttract)
	that.$u.vuex('vuex_isAllowWX', isAllowWX)
	that.$u.vuex('vuex_phone', phone)

	if (isAllowWX !== 1) {
		uni.reLaunch({
			url: "/pages/contract/contract"
		})
	} else {
		uni.reLaunch({
			url: "/pages/index/index"
		})
	}
	return data
}

//token 过期
const _getToken = async (that) => {
	console.log("传入的that",that)
	if (that.vuex_systemId) {
		//const data = await that.$u.http.post('Personnel/User/LoginWX?WXOpenId=' + that.vuex_openId)
		const data = await that.$u.http.post('Personnel/User/LoginWX',{WXOpenId:that.vuex_openId,Password:that.vuex_password,Account:that.vuex_account})
		console.log("code",data)
		
		if(data.code == 0){
				uni.reLaunch({
					url: "/pages/index/index"
				})
			}
		const {
			code,
			data: {
				roleId,
				account,
				id,
				name,
				isAllowAttract,
				isAllowWX,
				password,
				phone
			}
		} = data
		uni.setStorageSync('roleId', roleId)
		that.$u.vuex('vuex_account', account)
		that.$u.vuex('vuex_password', password)
		that.$u.vuex('vuex_Id', id)
		that.$u.vuex('vuex_Name', name)
		that.$u.vuex('vuex_isAllowAttract', isAllowAttract)
		that.$u.vuex('vuex_isAllowWX', isAllowWX)
		that.$u.vuex('vuex_phone', phone)

		const flag = await _login(that, phone, password)
	} else {
		uni.reLaunch({
			url: "/pages/login/login"
		})
	}

}
export {
	_login,
	_getOpenId,
	_getUserInfo,
	_getToken
}
